﻿<%@ WebHandler Language="C#" Class="list" %>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Text;
using System.Web.Script.Serialization;
using System.Web.SessionState;
using System.Web.Services;

public class list : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    private StringBuilder sb = null;
    public DataSet ds = null;
    public DataTable dt = null;
    private string sqlwhere = "";
    public void ProcessRequest (HttpContext context) {
        string type = PageBase.ext_string(context.Request.Params["type"]);

        switch (type)
        {
            //新增
            case "add":
                add(context);
                break;
            //加载数据
            case "getlist":
                getinfo_list(context);
                break;
            //删除
            case "delete":
                delete(context);
                break;
            //修改
            case "edit":
                edit(context);
                break;
            //根据id获取信息
            case "getlist_byid":
                getlist_byid(context);
                break;
            default:
                //getinfo(context);
                break;
        }
    }
    //修改
    public void edit(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string id = Utility.Trim(list.LoadData_type[0].id);
              string sbxh = Utility.Trim(list.LoadData_type[0].sbxh);
         string bwname = Utility.Trim(list.LoadData_type[0].bwname);
        string bwothername = Utility.Trim(list.LoadData_type[0].bwothername);
        string bwremark = Utility.Trim(list.LoadData_type[0].bwremark);
        string publicerrand = Utility.Trim(list.LoadData_type[0].publicerrand);
        string sbmaxvalue = Utility.Trim(list.LoadData_type[0].sbmaxvalue);
        string sbminvalue = Utility.Trim(list.LoadData_type[0].sbminvalue);
        string isactive = Utility.Trim(list.LoadData_type[0].isactive);
        string isactive_= isactive == "True" ? "Y" : "N";

        string ishave = "select * from t_bw_config where bwname='{0}' and isactive='Y'";
        ishave = string.Format(ishave, bwname);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0 && dtcount.Rows[0]["id"].ToString() != id)
        {
            flag = false;
            msg = "已存在此名称，请修改";

        }
        else
        {
            sqlsave = @"update [t_bw_config] set 
                                   [xh]='{0}'
                                  ,[bwname]='{1}'
                                  ,[bwothername]='{2}'
                                  ,[bwremark]='{3}'
                                  ,[publicerrand]='{4}'
                                  ,[maxvalue]='{5}'
                                  ,[minvalue]='{6}'
                                  ,[isactive]='{7}'
                                  ,[modifytime]='{8}'
                                   where id={9};";
            sqlsave = string.Format(sqlsave, sbxh, bwname, bwothername,bwremark,publicerrand, sbmaxvalue, sbminvalue, isactive_, DateTime.Now.ToString(),id);
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "修改成功";
            }
            else
            {
                flag = false;
                msg = "修改失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //查询部位信息
    public void getinfo_list(HttpContext context)
    {
        //页数1
        int page = PageBase.ext_int(context.Request.Params["page"]);
        //每页条数10
        int limit =PageBase.ext_int(context.Request.Params["limit"]);
        //排序字段
        string field =PageBase.ext_string(context.Request.Params["field"]);
        //排序方式
        string order = PageBase.ext_string(context.Request.Params["order"]);

        string orderby = "createtime";
        string where = @"select  COUNT(*) OVER(PARTITION BY 1) AS TOTAL,id, xh, bwname, bwothername, bwremark, publicerrand, maxvalue, 
           minvalue,  ltrim(rtrim(isactive)) as isactive, createtime, modifytime from 
                       [dbo].[t_bw_config]  where isactive='Y' and 1=1 ";
        string bwname = PageBase.ext_string(context.Request.Params["bwname"]);//部位名称
        string bwothername = PageBase.ext_string(context.Request.Params["bwothername"]);//部位简称

        //名称
        if (!string.IsNullOrEmpty(bwname))
        {
            where += " and bwname like N'%" + bwname + "%'";
        }
        //编号
        if (!string.IsNullOrEmpty(bwothername))
        {
            where += " and bwothername like N'%" + bwothername + "%'";
        }
        if (!string.IsNullOrEmpty(order) && !string.IsNullOrEmpty(field))
        {
            orderby =field + " " + order;
        }
        DataTable dt =  DBHelper.Pagination2(where, limit, page, orderby, "");

        int count = 0;
        if(dt.Rows.Count>0)
            count = PageBase.ext_int(dt.Rows[0]["TOTAL"].ToString());
        sb = new StringBuilder();
        string code = "0";
        string msg = "";
        sb.Append("{\"code\":\"" + code + "\",\"count\":" + count + ",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"xh\":\"" + dt.Rows[i]["xh"] + "\"");
            sb.Append(",\"bwname\":\"" + dt.Rows[i]["bwname"] + "\"");
            sb.Append(",\"bwothername\":\"" + dt.Rows[i]["bwothername"] + "\"");
            sb.Append(",\"bwremark\":\"" + dt.Rows[i]["bwremark"] + "\"");
            sb.Append(",\"publicerrand\":\"" + dt.Rows[i]["publicerrand"] + "\"");
            sb.Append(",\"maxvalue\":\"" + dt.Rows[i]["maxvalue"] + "\"");
            sb.Append(",\"minvalue\":\"" + dt.Rows[i]["minvalue"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"modifytime\":\"" + dt.Rows[i]["modifytime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }

    //查询特体信息根据id
    public void getlist_byid(HttpContext context)
    {
        string editid = PageBase.ext_string(context.Request.Params["editid"]);
        string sql = @"select id, xh, bwname, bwothername, bwremark, publicerrand, maxvalue, 
                      minvalue,  ltrim(rtrim(isactive)) as isactive, createtime, modifytime
                      from [t_bw_config] where id={0}";
        sql = string.Format(sql,editid);
        DataTable dt = DBHelper.ExecuteDataTable(sql);

        sb = new StringBuilder();
        string code = "0";
        string msg = "";

        sb.Append("{\"code\":\"" + code + "\",\"msg\":\"" + msg + "\",\"data\":[");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            sb.Append("{\"id\":\"" + dt.Rows[i]["id"].ToString() + "\"");
            sb.Append(",\"xh\":\"" + dt.Rows[i]["xh"] + "\"");
            sb.Append(",\"bwname\":\"" + dt.Rows[i]["bwname"] + "\"");
            sb.Append(",\"bwothername\":\"" + dt.Rows[i]["bwothername"] + "\"");
            sb.Append(",\"bwremark\":\"" + dt.Rows[i]["bwremark"] + "\"");
            sb.Append(",\"publicerrand\":\"" + dt.Rows[i]["publicerrand"] + "\"");
            sb.Append(",\"maxvalue\":\"" + dt.Rows[i]["maxvalue"] + "\"");
            sb.Append(",\"minvalue\":\"" + dt.Rows[i]["minvalue"] + "\"");
            sb.Append(",\"isactive\":\"" + dt.Rows[i]["isactive"] + "\"");
            sb.Append(",\"modifytime\":\"" + dt.Rows[i]["modifytime"] + "\"");
            sb.Append(",\"createtime\":\"" + dt.Rows[i]["createtime"] + "\"}");
            if (i < dt.Rows.Count - 1)
            {
                sb.Append(",");
            }
        }
        sb.Append("]}");
        context.Response.Write(sb.ToString());
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //删除
    public void delete(HttpContext context)
    {
        string getids = PageBase.ext_string(context.Request.Params["getids"]);
        string msg = "";
        bool flag;
        string sqldelete = "update t_bw_config set isactive='N' where id in ({0})";
        sqldelete = string.Format(sqldelete, getids);
        int dtcount= DBHelper.ExecuteNonQuery(sqldelete);
        if (dtcount > 0)
        {
            flag = true;
            msg = "删除成功";
        }
        else
        {
            flag = false;
            msg = "删除失败";

        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    //新增
    public void add(HttpContext context)
    {
        string msg = "";
        string sqlsave = "";
        bool flag;
        GetDate list = new GetDate();
        using (System.IO.StreamReader sr = new System.IO.StreamReader(context.Request.InputStream))
        {
            string s = sr.ReadToEnd();
            list = new JavaScriptSerializer().Deserialize<GetDate>(s);
        }
        string sbxh = Utility.Trim(list.LoadData_type[0].sbxh);
        string bwname = Utility.Trim(list.LoadData_type[0].bwname);
        string bwothername = Utility.Trim(list.LoadData_type[0].bwothername);
        string bwremark = Utility.Trim(list.LoadData_type[0].bwremark);
        string publicerrand = Utility.Trim(list.LoadData_type[0].publicerrand);
        string sbmaxvalue = Utility.Trim(list.LoadData_type[0].sbmaxvalue);
        string sbminvalue = Utility.Trim(list.LoadData_type[0].sbminvalue);
        string isactive = Utility.Trim(list.LoadData_type[0].isactive);
        string isactive_= isactive == "True" ? "Y" : "N";

        string ishave = "select * from t_bw_config where bwname='{0}' and isactive='Y'";
        ishave = string.Format(ishave, bwname);
        DataTable dtcount= DBHelper.ExecuteDataTable(ishave);
        if (dtcount.Rows.Count > 0)
        {
            flag = false;
            msg = "已存在此部位名称，请修改";
        }
        else
        {
            sqlsave = @"INSERT INTO [t_bw_config]
                                   ([xh]
                                   ,[bwname]
                                   ,[bwothername]
                                   ,[bwremark]
                                   ,[publicerrand]
                                   ,[maxvalue]
                                   ,[minvalue]
                                   ,[isactive]
                                   ,[createtime]
                                   )
                             VALUES
                                   ('{0}'
                                   ,'{1}'
                                   ,'{2}'
                                   ,'{3}'
                                   ,'{4}'
                                   ,'{5}'
                                   ,'{6}'
                                   ,'{7}'
                                   ,'{8}'
                                  );";
            sqlsave = string.Format(sqlsave, sbxh, bwname, bwothername,bwremark,publicerrand, sbmaxvalue, sbminvalue, isactive_, DateTime.Now.ToString());
            int zxcount = DBHelper.ExecuteNonQuery(sqlsave);
            if (zxcount > 0)
            {
                flag = true;
                msg = "新增成功";
            }
            else
            {
                flag = false;
                msg = "新增失败，请联系管理员";
            }
        }
        context.Response.Write("{\"flag\":\"" + flag + "\",\"remsg\":\"" + msg + "\"}");
        context.Response.ContentType = "text/json";
        context.Response.End();
    }
    public class GetDate
    {
        public List<typemanager> LoadData_type { get; set; }//总经理批示
    }
    public class typemanager
    {
        //id
        public string id;
        public string ID
        {
            get { return id; }
            set { id = value; }
        }
        //排序
        public string sbxh;
        public string SBXH
        {
            get { return sbxh; }
            set { sbxh = value; }
        }
        public string bwname;
        public string BWNAME
        {
            get { return bwname; }
            set { bwname = value; }
        }
        public string bwothername;
        public string BWOTHERNAME
        {
            get { return bwothername; }
            set { bwothername = value; }
        }
        public string bwremark;
        public string BWREMARK
        {
            get { return bwremark; }
            set { bwremark = value; }
        }
        public string publicerrand;
        public string PUBLICERRAND
        {
            get { return publicerrand; }
            set { publicerrand = value; }
        }
        public string sbmaxvalue;
        public string SBMAXVALUE
        {
            get { return sbmaxvalue; }
            set { sbmaxvalue = value; }
        }
        public string sbminvalue;
        public string SBMINVALUE
        {
            get { return sbminvalue; }
            set { sbminvalue = value; }
        }
        public string isactive;
        public string ISACTIVE
        {
            get { return isactive; }
            set { isactive = value; }
        }
    }


    public bool IsReusable {
        get {
            return false;
        }
    }

}